package cn.cs.service.impl;

import cn.cs.client.*;
import cn.cs.common.*;
import cn.cs.exception.ItemErrorCode;
import cn.cs.pojo.*;
import cn.cs.service.ItemDetailService;
import cn.cs.vo.ItemCommentVo;
import cn.cs.vo.ItemDetailVo;
import cn.cs.vo.ItemPriceVo;
import cn.cs.vo.ItemSchedulerVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

@Component
public class ItemDetailServiceImpl implements ItemDetailService {

    //@Autowired
    @Resource
    private RestCsProductClient restCsProductClient;

    //@Autowired
    @Resource
    private RestCsImageClient restCsImageClient;

  /*  @Autowired
    private RestDmCinemaClient restDmCinemaClient;*/

    //@Autowired
    @Resource
    private RestDmSchedulerClient restDmSchedulerClient;

    //@Autowired
    @Resource
    private RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;

/*
    @Autowired
    private RestDmItemCommentClient restDmItemCommentClient;
*/

    //@Autowired
    @Resource
    private RestCsUserClient restCsUserClient;

    @Autowired
    private RedisUtils redisUtils;

    private static final Logger logger = LoggerFactory.getLogger(ItemDetailServiceImpl.class);

    @Override
    public Dto<ItemDetailVo> queryProductDetail(Long id) throws Exception {
        logger.info("[queryItemDetail]开始查询id为" + id + "的商品详细信息");
        //查询对应ID的商品
        CsProduct csProduct = restCsProductClient.getCsProductById(id);
        if (EmptyUtils.isEmpty(csProduct)) {
            return null;
        }
        //查询图片信息
        String imgUrl = getImageUrl(restCsImageClient, Long.parseLong(csProduct.getId()), Constants.Image.ImageType.normal, Constants.Image.ImageCategory.item);
        //组装返回数据
        ItemDetailVo itemDetailVo = new ItemDetailVo();
        BeanUtils.copyProperties(csProduct, itemDetailVo);
        itemDetailVo.setImgUrl(imgUrl);
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }



    /**
     * 获取图片信息
     *
     * @param targetId
     * @param type
     * @param category     0:用户头像 1:商品图片
     * @param checkUserImg 是否是查询用户头像
     * @return
     * @throws Exception
     */
    public List<CsImage> getImageList(Long targetId, Integer type, Integer category, boolean checkUserImg) throws Exception {
        Map<String, Object> paramMapImage = new HashMap<String, Object>();
        paramMapImage.put("targetId", targetId);
        if (!checkUserImg) {
            paramMapImage.put("type", type);
        }
        //图片类型为1，代表查询商品图片
        paramMapImage.put("category", category);
        return restCsImageClient.queryCsImageList(targetId, type, category);
    }


  /*  *//**
     * 统一封装VO格式返回数据
     *
     * @param dmItem
     * @param dmCinema
     * @param imgUrl
     * @return
     *//*
    public ItemDetailVo copyData(CsProduct csProduct, DmCinema dmCinema, String imgUrl) throws ParseException {
        ItemDetailVo itemDetailVo = new ItemDetailVo();
        BeanUtils.copyProperties(dmItem, itemDetailVo);
        itemDetailVo.setStartTime(DateUtil.format(dmItem.getStartTime()));
        itemDetailVo.setEndTime(DateUtil.format(dmItem.getEndTime()));
        itemDetailVo.setState(dmItem.getState() + "");
        itemDetailVo.setImgUrl(imgUrl);
        if (EmptyUtils.isNotEmpty(dmCinema)) {
            BeanUtils.copyProperties(dmCinema, itemDetailVo);
        }
        itemDetailVo.setId(dmItem.getId());
        itemDetailVo.setCommentCount(dmItem.getCommentCount());
        return itemDetailVo;
    }*/

    /**
     * 查询图片信息
     *
     * @param restDmImageClient
     * @param type
     * @param category
     * @return
     */
    public String getImageUrl(RestCsImageClient restDmImageClient, Long id, Integer type, Integer category) {
        //查询图片信息
        List<CsImage> dmImageList = null;
        //从reidis中获取图片信息
        String key = Constants.IMAGE_TOKEN_PREFIX + id + "_" + type + "_" + category;
        String imgUrl = (String) redisUtils.get(key);
        //如果redis中没有,则到数据库中获取
        if (EmptyUtils.isEmpty(imgUrl)) {
            dmImageList = restDmImageClient.queryCsImageList(id, type, category);
            imgUrl = dmImageList.get(0).getImgUrl();
            //同步图片到redis
            redisUtils.set(key, imgUrl);
        }
        return imgUrl;
    }
}
